package com.huawei.zyb.taskManager.domain.requirement.entity;

import com.huawei.zyb.taskManager.domain.requirement.valueobject.RequirementId;
import com.huawei.zyb.taskManager.domain.requirement.valueobject.ReferenceDocumentId;
import com.huawei.zyb.taskManager.domain.requirement.valueobject.DocumentType;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 参考文档实体
 * 管理需求相关的参考文档信息
 */
public class ReferenceDocument {
    private ReferenceDocumentId id;
    private RequirementId requirementId;
    private String title;
    private String url;
    private String description;
    private DocumentType type;
    private String uploader; // 上传者
    private int viewCount; // 查看次数
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private LocalDateTime lastViewedAt;

    // 私有构造函数，强制使用工厂方法
    private ReferenceDocument() {}

    /**
     * 创建新的参考文档
     */
    public static ReferenceDocument create(
            ReferenceDocumentId id,
            RequirementId requirementId,
            String title,
            String url,
            String description,
            DocumentType type,
            String uploader) {
        
        Objects.requireNonNull(id, "文档ID不能为空");
        Objects.requireNonNull(requirementId, "需求ID不能为空");
        Objects.requireNonNull(title, "文档标题不能为空");
        Objects.requireNonNull(url, "文档链接不能为空");
        Objects.requireNonNull(type, "文档类型不能为空");
        
        if (title.trim().isEmpty()) {
            throw new IllegalArgumentException("文档标题不能为空字符串");
        }
        
        if (url.trim().isEmpty()) {
            throw new IllegalArgumentException("文档链接不能为空字符串");
        }
        
        ReferenceDocument document = new ReferenceDocument();
        document.id = id;
        document.requirementId = requirementId;
        document.title = title.trim();
        document.url = url.trim();
        document.description = description != null ? description.trim() : "";
        document.type = type;
        document.uploader = uploader != null ? uploader.trim() : "";
        document.viewCount = 0;
        document.createdAt = LocalDateTime.now();
        document.updatedAt = LocalDateTime.now();
        
        return document;
    }

    /**
     * 从数据库数据恢复参考文档对象
     */
    public static ReferenceDocument restore(
            ReferenceDocumentId id,
            RequirementId requirementId,
            String title,
            String url,
            String description,
            DocumentType type,
            String uploader,
            int viewCount,
            LocalDateTime createdAt,
            LocalDateTime updatedAt,
            LocalDateTime lastViewedAt) {
        
        ReferenceDocument document = new ReferenceDocument();
        document.id = id;
        document.requirementId = requirementId;
        document.title = title;
        document.url = url;
        document.description = description;
        document.type = type;
        document.uploader = uploader;
        document.viewCount = viewCount;
        document.createdAt = createdAt;
        document.updatedAt = updatedAt;
        document.lastViewedAt = lastViewedAt;
        
        return document;
    }

    /**
     * 更新文档信息
     */
    public void updateInfo(String title, String url, String description, DocumentType type) {
        Objects.requireNonNull(title, "文档标题不能为空");
        Objects.requireNonNull(url, "文档链接不能为空");
        Objects.requireNonNull(type, "文档类型不能为空");
        
        if (title.trim().isEmpty()) {
            throw new IllegalArgumentException("文档标题不能为空字符串");
        }
        
        if (url.trim().isEmpty()) {
            throw new IllegalArgumentException("文档链接不能为空字符串");
        }
        
        this.title = title.trim();
        this.url = url.trim();
        this.description = description != null ? description.trim() : "";
        this.type = type;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 记录文档查看
     */
    public void recordView() {
        this.viewCount++;
        this.lastViewedAt = LocalDateTime.now();
    }

    /**
     * 检查是否为在线文档
     */
    public boolean isOnlineDocument() {
        return url.startsWith("http://") || url.startsWith("https://");
    }

    /**
     * 获取文档显示名称
     */
    public String getDisplayName() {
        return title + " (" + type.getDescription() + ")";
    }

    // Getters
    public ReferenceDocumentId getId() { return id; }
    public RequirementId getRequirementId() { return requirementId; }
    public String getTitle() { return title; }
    public String getUrl() { return url; }
    public String getDescription() { return description; }
    public DocumentType getType() { return type; }
    public String getUploader() { return uploader; }
    public int getViewCount() { return viewCount; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public LocalDateTime getUpdatedAt() { return updatedAt; }
    public LocalDateTime getLastViewedAt() { return lastViewedAt; }

    /**
     * 增加查看次数
     */
    public void incrementViewCount() {
        this.viewCount++;
        this.lastViewedAt = LocalDateTime.now();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        ReferenceDocument that = (ReferenceDocument) obj;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "ReferenceDocument{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", type=" + type +
                ", viewCount=" + viewCount +
                "}";
    }
}